//
//  ArticleTableViewController.swift
//  LearnDemo
//
//  Created by 振轩 on 2018/4/8.
//  Copyright © 2018年 振轩. All rights reserved.
//

import UIKit
let articleCell = "articleCell"
let articleVideo = "articleVideo"
class ArticleTableViewController:SwiftBaseTableViewController {
   
    var rowH:CGFloat = 0
    var data:ReList?

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        loadData()
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return就算晚安
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if section == 0{
            return (data?.videoList!.count) ?? 0
            
        }else{
            return (data?.articleList!.count) ?? 0
           
        }
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 1{
        let cell = tableView.dequeueReusableCell(withIdentifier: articleCell, for: indexPath) as! ArticleTableViewCell
            cell.mode = self.data?.articleList![indexPath.row] as? articleSub
            
            
        return cell
            
        }else{
            
            let cell = tableView.dequeueReusableCell(withIdentifier: articleVideo, for: indexPath) as! ArticleVideoTableViewCell
            cell.mode = data?.videoList![indexPath.row] as? videoSub
            cell.MoreBTN.addTarget(self, action: #selector(gotoDetail), for: .touchUpInside)
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 253
        }else{
            return 120
            
        }
    }
    //加在数据
    override func loadData(){
        let params = NSMutableDictionary()
        
       let API:BaseReqApi = BaseReqApi.init(requestUrl: "/index.php/FoodCheckApi/courseHot.json", andrequestTime: 5, andParams: params as! [AnyHashable : Any], andRequestMethod: YTKRequestMethod.POST, andCache: false, andCacheTime: 0, andPostToken: true)
        
        API.starRequest { (status, mess, response) in
            
             guard let dic:Dictionary = response as? [String : AnyObject] else{
                
                return
            }
            
            print(dic)
      
            
            if status.rawValue == 1  {
                
                
                
                let mode1  = RecMode.init(DIC: dic)
                
                self.data = mode1.data as? ReList
                //self.data?.articleList?.removeAll()
                //self.data?.videoList?.removeAll();
              
                if (self.data?.articleList?.count == 0 && self.data?.videoList?.count == 0 ){
                
                    self.Datatype = REQuestDataType.NODATAType
                    
                
                }else{
                    
                     self.Datatype = REQuestDataType.NORMALLType
                }

                
                self.tableView.reloadData();
               
                }
            
            
            
            
            
            
            
            
            
            
            
        }
        
       print(API)
    }
    func next(){
        
        
        
    }
    
    @objc func gotoDetail(){
        
        
          navigationController?.pushViewController(LearnMainViewController(), animated: true)
        
    }
    
}


extension ArticleTableViewController{
    func setUpUI(){
        
        tableView.register(UINib(nibName: "ArticleTableViewCell", bundle: nil), forCellReuseIdentifier: articleCell)
        
        tableView.register(UINib(nibName: "ArticleVideoTableViewCell", bundle: nil), forCellReuseIdentifier: articleVideo)
        //tableView.rowHeight = 120
        //header.frame = CGRect(x: 0, y: 100, width: kScreenW, height: 250)
        navigationItem.title = "在线学习"
        
        //tableView.tableHeaderView  = header
        tableView.frame = CGRect(x: 0, y: 320, width: kScreenW, height: kScreenH-320)
        
        tableView.tableFooterView = UIView()
        
      
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            //navigationController?.pushViewController(LearnMainViewController(), animated: true)
            
            
            
            let mode :videoSub = (data?.videoList![0])! as! videoSub
            let vc = LiveViewControllerViewController()
            let mode1:ReqMode = ReqMode()
            
            mode1.courseId = mode.id
            
            mode1.staffId = LoginStatuesModel.getStaffId()
            mode1.staffType = LoginStatuesModel.getUserType()
            vc.ReqM = mode1
            present(vc, animated: true, completion: nil)
            
        }else{
            
            
            let mode = data?.articleList![indexPath.row] as?articleSub
            
                let vc = ArticleDetailTableViewController()
                let reqM = ReqMode()
            reqM.courseId = (mode?.id)!
                reqM.staffId = LoginStatuesModel.getStaffId()
                reqM.staffType = LoginStatuesModel.getUserType()
            
                vc.ReqM = reqM
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section == 1 {
            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 40))
            //headerView.backgroundColor = UIColor.orange
            let btn = UIButton.init(frame: CGRect(x: -15, y: 0, width: 120, height: 40))
            btn.setTitle(" 行业资讯", for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.addTarget(self, action: #selector(moreData), for: .touchUpInside)
            
            btn.setImage(UIImage.init(named: "zx"), for: .normal)
            headerView.addSubview(btn)
            headerView.backgroundColor = UIColor.white
            let lineView = UIView(frame: CGRect(x: 10, y: 39, width: kScreenW-20, height: 1))
            lineView.backgroundColor = UIColor.groupTableViewBackground
            
            let more = UIButton.init(frame: headerView.bounds)
            more.addTarget(self, action: #selector(moreData), for: .touchUpInside)
            headerView.addSubview(more)
            
            let imgView = UIImageView.init(frame: CGRect(x: kScreenW - 22, y: 14, width: 10, height: 12))
            imgView.image = UIImage(named: "groupA")
            
            let lab = UILabel(frame: CGRect(x: kScreenW - 100, y: 0, width: 66, height: 40))
            lab.text = "更多"
            lab.textAlignment = NSTextAlignment.right
            lab.font = UIFont.systemFont(ofSize: 14)
            
            headerView.addSubview(lab)
            headerView.addSubview(imgView)
            headerView.addSubview(lineView)
             return headerView
        }else{
            
            let view = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            return view
        }
       
        
    }
    
    @objc func moreData(){
        navigationController?.pushViewController(InformationViewController(), animated: true)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        }else{
            
            return 40
        }
    }
   
    

}






//数据模型。

class RecMode: NSObject {
    @objc var code:NSInteger = 0
   
    @objc var msg:String?
    @objc var data:AnyObject?{
        
        didSet{
            
            let mode = ReList.init(DIC: data as! [String : AnyObject])
           data  = mode
            
            
        }
        
    }

  convenience init(DIC:[String:AnyObject]) {
        self.init()

        setValuesForKeys(DIC)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {

    }
}





class ReList: NSObject {
    
    @objc var articleList:[AnyObject]?{
        didSet{
            
            var videoLists = [articleSub]()
            for sub in articleList!{
                
                let s = articleSub.init(DIC: sub as! [String : AnyObject])
                videoLists.append(s)
                
            }
          articleList = videoLists
            
            
            
        }
        
        
    }
    @objc var videoList:[AnyObject]?{
        didSet{
            
            var videoLists = [videoSub]()
            for sub in videoList!{
                
                let s = videoSub.init(DIC: sub as! [String : AnyObject])
               videoLists.append(s)
                
            }
            videoList = videoLists
            
            
            
        }
    
    }

    
    convenience init(DIC:[String:AnyObject]) {
        self.init()

        setValuesForKeys(DIC)
    }
    
//    override func setValue(_ value: Any?, forUndefinedKey key: String) {
//
//    }
}
class videoSub: NSObject {
 @objc  var collectNum:NSInteger = 0
 @objc   var ROW_NUMBER:NSInteger = 0
 @objc   var addDate:String?
 @objc    var id:NSInteger = 0
 @objc   var intro:String?
 @objc   var likeNum:NSInteger = 0
 @objc   var  thumb:String?
 @objc   var title :String?
 @objc  var type:NSInteger = 0
 @objc   var viewNum :NSInteger = 0
 convenience init(DIC:[String:AnyObject]) {
        self.init()

        setValuesForKeys(DIC)
    }

}
class articleSub:NSObject{
  @objc  var ROW_NUMBER:NSInteger = 0
  @objc   var addDate:String?
  @objc  var collectNum:NSInteger = 0
  @objc  var id:NSInteger = 0
  @objc  var intro:String?
  @objc  var likeNum:NSInteger = 0
  @objc  var  thumb:String?
  @objc  var title :String?
  @objc  var type:NSInteger = 0
  @objc  var viewNum :NSInteger = 0
  convenience init(DIC:[String:AnyObject]) {
       self.init()

       setValuesForKeys(DIC)
   }
    
}


class BaseM: NSObject {
    
}

